package com.kye.express.business.jd_reservation.edit;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kye.express.R;
import com.kye.express.databinding.ItemJdOrderEditEditCodeBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;

import java.util.List;

import me.drakeet.multitype.ItemViewBinder;

public class ItemEditViewBinder extends ItemViewBinder<ItemEdit, ItemEditViewBinder.ViewHolder> {

    private TextWatcher mTextWatcher;
    private OnItemClickListener<Integer> mOnItemClickListener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_jd_order_edit_edit_code, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ItemEdit item) {

    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ItemEdit item, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, item, payloads);
        holder.bind(item, getAdapter().getItemCount(), payloads);
    }

    public TextWatcher getTextWatcher() {
        return mTextWatcher;
    }

    public void setLastTextWatcher(TextWatcher textWatcher) {
        mTextWatcher = textWatcher;
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        mTextWatcher = textWatcher;
    }

    public OnItemClickListener<Integer> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemDeleteClickListener(OnItemClickListener<Integer> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemJdOrderEditEditCodeBinding mBind;
        private boolean mLast;
        private ItemEdit mItemEdit;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemJdOrderEditEditCodeBinding.bind(itemView);

            mBind.editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    if (!mLast || mTextWatcher == null) return;

                    mTextWatcher.beforeTextChanged(s, start, count, after);
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    mItemEdit.setText(s.toString());

                    if (!mLast || mTextWatcher == null || TextUtils.isEmpty(s)) return;

                    mTextWatcher.onTextChanged(s, start, before, count);
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!mLast || mTextWatcher == null) return;

                    mTextWatcher.afterTextChanged(s);
                }
            });

            mBind.imageButtonDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener == null) return;

                    mOnItemClickListener.onClickItem(getAdapterPosition(), v);
                }
            });
        }

        public void bind(ItemEdit itemEdit, int itemCount, List<Object> payloads) {
            mItemEdit = itemEdit;
            mLast = itemCount - 1 == getAdapterPosition();
            mBind.setShowClose(!mLast);
            mBind.setHintText(itemEdit.getHintText());

            if (payloads.isEmpty()) {
                mBind.editText.setText(itemEdit.getText());
                mBind.editText.setSelection(mBind.editText.getText().length());
            }
        }
    }
}
